package com.ruoyi.monitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dyun.tool.mybatis.domain.PageDomain;
import com.ruoyi.common.annotation.SysLog;
import com.ruoyi.common.enums.BusinessTypeEnum;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.quartz.entity.SysJob;
import com.ruoyi.quartz.serviece.ISysJobService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import net.dreamlu.mica.core.result.R;
import net.dreamlu.mica.core.utils.$;
import net.dreamlu.mica.core.validation.CreateGroup;
import net.dreamlu.mica.core.validation.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务调度 控制层
 *
 * @author dy
 */
@RestController
@RequestMapping("/monitor/job")
@AllArgsConstructor
public class SysJobController extends BaseController {

    private final ISysJobService sysJobService;

    /**
     * 分页
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public R<IPage<SysJob>> list(SysJob sysJob, PageDomain pageDomain) {
        IPage<SysJob> pages = sysJobService.page(startPage(pageDomain), new LambdaQueryWrapper<SysJob>()
                .like($.isNotBlank(sysJob.getJobName()), SysJob::getJobName, sysJob.getJobName())
                .eq($.isNotBlank(sysJob.getJobGroup()), SysJob::getJobGroup, sysJob.getJobGroup())
                .eq($.isNotNull(sysJob.getStatus()), SysJob::getStatus, sysJob.getStatus())
                .like($.isNotBlank(sysJob.getInvokeTarget()), SysJob::getInvokeTarget, sysJob.getInvokeTarget()));
        return success(pages);
    }

    /**
     * 详情
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping("/detail")
    public R<SysJob> detail(Long id) {
        return success(sysJobService.getById(id));
    }

    /**
     * 新增
     */
    @SysLog(title = "定时任务", businessType = BusinessTypeEnum.INSERT)
    @SneakyThrows
    @PreAuthorize("@ss.hasPermi('monitor:job:add')")
    @PostMapping("/add")
    public R add(@Validated(value = {CreateGroup.class}) @RequestBody SysJob sysJob) {
        if (!sysJobService.checkCronExpressionIsValid(sysJob.getCronExpression())) {
            return fail("cron表达式不正确");
        }
        return status(sysJobService.insertJob(sysJob));
    }

    /**
     * 修改
     */
    @SysLog(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @SneakyThrows
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @PutMapping("/update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody SysJob sysJob) {
        if (!sysJobService.checkCronExpressionIsValid(sysJob.getCronExpression())) {
            return fail("cron表达式不正确");
        }
        return status(sysJobService.updateJob(sysJob));
    }

    /**
     * 定时任务状态修改
     */
    @SysLog(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @SneakyThrows
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody SysJob job) {
        SysJob newJob = sysJobService.getById(job.getId());
        newJob.setStatus(job.getStatus());
        return status(sysJobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @SysLog(title = "定时任务", businessType = BusinessTypeEnum.UPDATE)
    @SneakyThrows
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @PutMapping("/run")
    public R run(@RequestBody SysJob job) {
        return status(sysJobService.run(job));
    }

    /**
     * 删除
     */
    @SysLog(title = "定时任务", businessType = BusinessTypeEnum.DELETE)
    @SneakyThrows
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable Long[] ids) {
        return status(sysJobService.deleteJobByIds(ids));
    }
}